<template>
  <div class="commentItem">
    <div class="top-wrap">
      <div class="avatar">
        <img v-lazy="item.avatar" alt @error="imgError" />
      </div>
      <div class="content">
        <div class="userInfo">
          <p class="nickname">
            {{ item.userName || '-' }}
          </p>
          <p class="time">
            {{ item.createdTime || '' }}
          </p>
        </div>
        <div class="starWrap">
          <van-rate
            :value="Number(item.serviceOverallStar || 0)"
            :allow-half="true"
            :size="9"
            :gutter="4"
            color="#FFB223"
            readonly
            void-icon="star"
            void-color="#d5d5d5"
          />
        </div>
      </div>
    </div>
    <div class="comments-wrap">
      <div
        ref="commentsRef"
        :class="{ 'comment-text': showCollapse && !isExpand }"
      >
        {{ item.content || '' }}
      </div>
      <div v-if="showCollapse" class="hideAndShow">
        <div @click="switchCollapse">
          {{ !isExpand ? '展开' : '收起' }}
          <van-icon :name="!isExpand ? 'arrow-down' : 'arrow-up'" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    // 评论数据项
    item: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      showCollapse: false, // 是否展示折叠按钮
      isExpand: false // 是否展开状态
    };
  },
  mounted() {
    this.$nextTick(() => {
      const commentsRef = this.$refs.commentsRef;
      this.showCollapse = commentsRef && this.countLines(commentsRef) > 3;
    });
  },
  methods: {
    // 头像容错
    imgError(e) {
      e.target.src = '';
    },
    // 获取擅长的行数
    countLines(ele) {
      console.log('countLines==ele==', ele);
      const styles = window.getComputedStyle(ele, null);
      const lh = parseInt(styles.lineHeight, 10);
      const h = parseInt(styles.height, 10);
      const lc = Math.round(h / lh);
      console.log('line count:', lc, 'line-height:', lh, 'height:', h);
      return lc;
    },
    switchCollapse() {
      this.isExpand = !this.isExpand;
    }
  }
};
</script>
<style lang="scss" scoped>
.commentItem {
  position: relative;
  padding: 60px 30px 0;

  .top-wrap {
    display: flex;
    align-items: center;
  }

  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 12px;

    img {
      border-radius: 50%;
    }
  }

  .content {
    flex: 1;
    width: 0;

    .userInfo {
      display: flex;
      align-items: center;

      .nickname {
        flex: 1;
        width: 0;
        font-size: 30px;
        color: #333;
        word-break: break-all;
      }

      .time {
        margin-top: 2px;
        font-size: 24px;
        color: #999;
      }
    }

    .starWrap {
      margin-top: 6px;
      font-size: 0;
    }
  }

  .comments-wrap {
    margin-top: 22px;
    font-size: 28px;
    line-height: 40px;
    color: #333;
    word-break: break-all;
    word-wrap: break-word;
    // word-wrap（overflow-wrap）

    .comment-text {
      display: -webkit-box;
      overflow: hidden;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
    }

    .hideAndShow {
      display: flex;
      justify-content: flex-end;
      padding-top: 4px;
      font-size: 24px;
      font-weight: 500;
      color: #00c5cd;

      div {
        display: flex;
        align-items: center;
      }
    }
  }
}

::v-deep .van-rate__icon {
  font-size: 25px;
}
</style>
